<!--  -->
<script setup>
import { useRoute } from 'vue-router'
import { watch, ref, nextTick } from 'vue'

const route = useRoute()
const cate = ref(route.params.category)
const props = defineProps({ articleList: Object })
const dirs = ref([])
const file = ref([])

watch(() => props.articleList, init)

function init() {
  dirs.value.length = 0
  file.value.length = 0
  props.articleList.forEach((v) => {
    if (v.type && v.type == 1) dirs.value.push(v)
    else file.value.push(v)
  })
  tryFindActiveShard()
}
const currentShard = ref('')
const currentArticle = ref('')

watch(
  () => route.params.category,
  newCate => cate.value = newCate
)
watch(
  () => route.params.id,
  newId => currentArticle.value = newId
)

nextTick(() => currentArticle.value = route.params.id)

function tryFindActiveShard() {
  const currentId = route.params.id
  for (const v of dirs.value) {
    if (v.type == 1 && v.children && v.children.some(c => c.id == currentId)) {
      currentShard.value = v.id
      break
    }
  }
}

function toggleTable(e) {
  const span = e.target
  if (span.classList.contains('open')) {
    span.classList.remove('open')
    span.classList.add('closed')
    span.parentNode.nextElementSibling.classList.remove('show')
    span.parentNode.nextElementSibling.classList.add('hide')
  }
  else {
    span.classList.remove('closed')
    span.classList.add('open')
    span.parentNode.nextElementSibling.classList.add('show')
    span.parentNode.nextElementSibling.classList.remove('hide')
  }
}
</script>

<template>
  <div class="sidebar-wrapper invisible-scrollbar">
    <RouterLink :to="{ path: `/article/${cate}/index` }" @click="currentArticle = ''">
      <div class="sidebar-title">
        {{ $t('view_article.menu_prompt') }}
      </div>
    </RouterLink>
    <ul class="shard-content" v-if="dirs.length">
      <li class="" v-for="v in dirs" :key="v.id">
        <div class="shard-item" :class="{ 'shard-link-active': v.id == currentShard }">
          <div class="icon icon-folder"></div>
          <div class="shard-item-title closed" @click="toggleTable" :data-id="v.id">
            {{ v.title }}
          </div>
          <div class="shard-item-count">{{ v.children.length }}</div>
        </div>
        <div v-if="!v.children.length" class="hide shard-empty">空的喵</div>
        <div class="shard-item-content hide">
          <div v-for="sv in v.children" :key="sv.id" class="sidebar-link">
            <RouterLink :to="{ path: `/article/${cate}/${sv.id}` }" class="link-item shard-item-link"
              :class="{ 'link-active': sv.id === currentArticle }" @click="currentArticle = sv.id, currentShard = v.id">
              {{ sv.title }}
            </RouterLink>
          </div>
        </div>
      </li>
    </ul>
    <ul class="sidebar-content">
      <li v-for="v in file" :key="v.id" class="sidebar-link" @click="currentArticle = v.id, currentShard = ''">
        <RouterLink class="link-item" :to="{ path: `/article/${cate}/${v.id}` }"
          :class="{ 'link-active': v.id === currentArticle }">
          {{ v.title }}
        </RouterLink>
      </li>
    </ul>
    <div class="sidebar-empty"></div>
  </div>
</template>

<style scoped lang="scss">
.sidebar-wrapper {
  position: sticky;
  top: 30px;
  max-height: 100vh;
  overflow-y: auto;
}

.invisible-scrollbar::-webkit-scrollbar {
  display: none;
}

.sidebar-title {
  color: var(--primary-tc);
  margin: 10px 2px;
  text-align: center;
  line-height: 3rem;

  &:hover {
    text-decoration: underline 2px rgb(110, 223, 144);
  }
}

.icon {
  mask-repeat: no-repeat;
  mask-position: center;
  display: inline-block;
}

.icon-folder {
  width: 15px;
  height: 15px;
  margin: auto 4px;
  mask-image: url('/static/svg/folder.svg');
  background-color: #75972b;
}

.shard-content {
  background-color: #4099400e;
  border-top: 1px solid var(--border-secondary);
  border-bottom: 1px solid var(--border-secondary);

  .show {
    display: block;
  }

  .hide {
    display: none;
  }

  .open {
    background: var(--linear-01) !important;
  }
}

.shard-item {
  height: 100%;
  min-height: 2.5rem;
  display: flex;
  align-items: center;
  cursor: default;

  .shard-item-title {
    flex: 1;
    padding: .6rem 5px;

    &:hover {
      background: rgba(181, 195, 159, 0.431);
    }
  }

  .shard-item-count {
    margin: 0 2px;
    color: var(--side-item-tc);
    font-family: "Oswald", "Bookman Old Style";
  }
}

.shard-empty {
  margin-left: 26px;
  color: var(--side-item-tc);
  height: 100%;
  padding: .75rem 2px .75rem .5rem;
  margin-left: 23px;
  font-family: "Oswald", "Bookman Old Style";
  border-left: 1px solid rgb(12, 114, 12);
  border-bottom: 1px solid #7b7ea4;
  min-height: 3rem;
}

.shard-item-title {
  padding-left: 5px;
  font-family: "Oswald", "Bookman Old Style";
}

.shard-item-link {
  margin-left: 23px;
  border-left: 1px solid rgb(12, 114, 12);
}


.sidebar-empty {
  height: 50vh;
}

.link-item {
  display: block;
  color: var(--side-item-tc);
  height: 100%;
  padding: .75rem 2px .75rem .5rem;
  font-family: "Oswald", "Bookman Old Style";

  border-bottom: 1px solid #7b7ea4;
  cursor: pointer;
  min-height: 3rem;

  &:hover {
    background: rgba(159, 195, 169, 0.431);
  }
}

.shard-link-active {
  font-weight: bold;
  text-decoration: underline 2px var(--text-02);
  color: var(--text-02)
}

.link-active {
  font-weight: bold;
  color: #033b00;
  background: var(--linear-02) !important;
}
</style>
